/* Bookmarklet code书签脚本代码
javascript: (function() {
  if (!window.Bookmarklet) {
    var a = document.createElement("script");
    a.setAttribute("src", "http://localhost:8081/imagelister.js");
    document.getElementsByTagName("body")[0].appendChild(a);
    a.onload = function() {
      Bookmarklet.ImageLister(window);
    };
  } else {
    Bookmarklet.showImageLister();
  }
})(); */
var Bookmarklet={}
Bookmarklet.template=[
	"[item]<br>",
	"[img][item][/img]<br><br>",
	"<a href='[item]' >[item]</a><br>",
	"<img src='[item]' alt='[item]'/><br><br>",
	"<a href='[item]'><img src='[item]' alt='[item]'/></a><br><br>",
	"<a href='[item]'><img src='[item]' width='100px' alt='[item]'/></a>"
];
Bookmarklet.showImageLister=function(){bml_outputdiv.ondblclick();}

Bookmarklet.readcookie=function(k){
	RegExp.multiline=true;
	if(document.cookie){
		var r=new RegExp('('+k+')=([^=;]*)($|;)',"ig");
		var a=r.exec(document.cookie);
		if(a){return unescape(a[2]);}		
	}
	return '';	
}
Bookmarklet.writecookie=function (ak,av){
	var k=ak;
	var v=av;
	RegExp.multiline=true;
	if(document.cookie){
		var r=new RegExp('('+k+')=([^=;]*)($|;)',"ig");
		var a=r.exec(document.cookie);
		if(a){
			if(document.cookie.length>4000){return false;}
		}else{
			if (document.cookie.length+escape(v).length>4000){return false;}
		}		
	}else{
		if(escape(v).length>4000){return false;}				
	}
	document.cookie=k+'='+escape(v);
	return true;
}
Bookmarklet.writeaddcookie=function (ak,av){
	var k=ak;
	var v=av;
	var s=this.readcookie(k);
	return this.writecookie(k,s+v);
}
Bookmarklet.ImageLister=function(w) {
    //初始化部分变量
    var d = window.document;
    var titlestr = '';
    var out = d.createElement('div');
	
    out.id = 'bml_outputdiv';
    out.ondblclick = function(e) {
        this.style.display = (this.style.display == 'inline') ? 'none': 'inline';
    };
	 out.setAttribute('class',"ilpanel");
    out.setAttribute('style', 'position:absolute;z-index:900;width:90%;height:90%;border:0.2cm groove black;background-color:#eeffee;color:black;overflow-y:auto;text-align:left');
    out.innerHTML = '<input type="text" id="bml_templatetxt" style="width:250px" name="bml_templatetxt" > \
	<select id="bml_templateid" onchange="bml_templatetxt.value=Bookmarklet.template[this.value];"> \
					<option value=0>address text</option> \
					<option value=1>address ubb</option> \
					<option value=2 selected>address link</option> \
					<option value=3>img</option> \
					<option value=4>img&link</option> \
					<option value=5>thumbnail&link</option> \
					</select> \
					<input id="btncookiesave" type="button" value="S" title="Save as Default" onclick="Bookmarklet.writecookie(\'tmpId\',bml_templateid.value);"> \
					<input type="text" id="bml_pattentxt" style="width:100px" name="bml_pattentxt" > \
					<input  type="text" id="bml_replacetxt" style="width:100px" name="bml_replacetxt" > \
					<input type="button" value="P" title="Temporary save" \
					onclick="if(Bookmarklet.writeaddcookie(\'bmldata\',bml_output.innerHTML)){bml_output.innerHTML=\'Save complite\';Bookmarklet.writecookie(\'tcounter\',parseInt(Bookmarklet.readcookie(\'tcounter\'))+parseInt(bml_ttlink.innerHTML.match(/\\d+/ig)[0] ))};"> \
					<input type="button" value="G" title="Retrieve" \
					onclick="bml_output.innerHTML=Bookmarklet.readcookie(\'bmldata\');bml_ttlink.innerHTML=\'[\'+Bookmarklet.readcookie(\'tcounter\') +\'P]\'"> \
					<input type="button" value="C" title="Clear" \
					onclick="Bookmarklet.writecookie(\'bmldata\',\' \');Bookmarklet.writecookie(\'tcounter\',0)"> \
					<input type="button" value="S" title="Share" \
					onclick="Bookmarklet.writecookie(\'bmldata\',\' \');Bookmarklet.writecookie(\'tcounter\',0)"> \
					<div id="bml_ttlink">title</div> \
					<div id="bml_output" name="bml_output" style="width:80%;height:500px;overflow-y:auto;">temp</div>';
    var b = d.getElementsByTagName('body')[0];
    b.insertBefore(out, b.children[0]);
    //构建界面完毕，开始初始化控件
	
    var ctmp = "";
    var tmpid=this.readcookie('tmpId');
     bml_templateid.value = tmpid==''?2:parseInt(tmpid);
    bml_templatetxt.value = Bookmarklet.template[bml_templateid.value];
    var s = [];
    for (var i = 0; i < d.images.length; i++) {
        //将去重部分加入循环
        if (s.indexOf(d.images[i].src) == -1) {
            s.push(d.images[i].src);
        }
    }	
	bml_outputdiv.style='top:100px;position:absolute;z-index:900;width:90%;height:90%;border:0.2cm groove black;background-color:#eeffee;color:black;overflow-y:auto;text-align:left';
    bml_output.innerHTML = s.toString().replace(/,/ig, "<br>");
	bml_output.oncontextmenu=function(){return false; } 
    bml_output.onmouseup = function(e) {
        if (e.button == 0) {
            var o = [];
            var pat = window.getSelection();
            var tmpstr = bml_templatetxt.value;
            if (pat == "" || pat.toString().indexOf('\n') >= 0) return;
            for (var i = 0; i < s.length; i++) {
                if (s[i].indexOf(pat) >= 0) {
                    o.push(tmpstr.replace(/\[item\]/ig, s[i]).replace(new RegExp(bml_pattentxt.value, "ig"), bml_replacetxt.value));
                };
            };

            bml_output.innerHTML = o.toString().replace(/,/ig, '')+"\n";
            bml_ttlink.innerHTML = titlestr + "[" + o.length + "P]";

        } else if (e.button == 2) {
            bml_output.innerHTML = s.toString().replace(/,/ig, "<br>");
			return null;
        } else{alert(e.button)}
    }
	document.onscroll=function(e){
		var s = Math.max(document.body.scrollTop, document.documentElement.scrollTop);		
		bml_outputdiv.style.top=s +'px';
	};document.onscroll(window.event);
}
